<template>
  <div class="flight-item">
    <div @click="handleShowFlightsList">
      <!-- 显示的机票信息 -->
      <el-row type="flex" align="middle" class="flight-info">
        <el-col :span="6">
          <span>{{data.airline_name}}</span>
          {{data.flight_no}}
        </el-col>
        <el-col :span="12">
          <el-row type="flex" justify="space-between" class="flight-info-center">
            <el-col :span="8" class="flight-airport">
              <strong>{{data.dep_time}}</strong>
              <span>{{data.org_airport_name}}{{data.org_airport_quay}}</span>
            </el-col>
            <el-col :span="8" class="flight-time">
              <!-- 时间转换为分钟再进行计算 -->
              <span>{{timeDifference}}</span>
            </el-col>
            <el-col :span="8" class="flight-airport">
              <strong>{{data.arr_time}}</strong>
              <span>{{data.dst_airport_name}}{{data.dst_airport_quay}}</span>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="6" class="flight-info-right">
          ￥
          <span class="sell-price">{{data.base_price / 2}}</span>起
        </el-col>
      </el-row>
    </div>
    <div class="flight-recommend" v-show="showFlightsList">
      <!-- 隐藏的座位信息列表 -->
      <el-row type="flex" justify="space-between" align="middle">
        <el-col :span="4">低价推荐</el-col>
        <el-col :span="20">
          <el-row
            type="flex"
            justify="space-between"
            align="middle"
            class="flight-sell"
            v-for="(item,index) in data.seat_infos"
            :key="index"
          >
            <el-col :span="16" class="flight-sell-left">
              <span>{{item.name}}</span>
              | {{item.supplierName}}
            </el-col>
            <el-col :span="5" class="price">￥{{item.org_settle_price}}</el-col>
            <el-col :span="3" class="choose-button">
              <nuxt-link :to="`/air/order?id=${data.id}&seat_xid=${item.seat_xid}`">
                <el-button type="warning" size="mini">选定</el-button>
              </nuxt-link>
              <p>剩余：{{item.discount}}</p>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
export default {
  props: [
    //   接收父组件绑定事件
    "data"
  ],
  data() {
    return {
      showFlightsList: false
    };
  },
  computed: {
    // 计算相差时间
    // 分别获取出发时间和到达时间
    timeDifference() {
      const depTime = this.data.dep_time.split(":");
      const arrTime = this.data.arr_time.split(":");
      // 分别将分割好的时间计算成分钟然后相减  因为是字符串相加会直接拼接到后面，所以我们要先转换为数值
      const depVal = depTime[0] * 60 + +depTime[1];
      const arrVal = arrTime[0] * 60 + +arrTime[1];

      // 计算差值
      let discountTime = arrVal - depVal;
      // 如果飞机是零点之后到达的，我们要进行处理 让当前时间加一天
      if (discountTime < 0) {
        discountTime = arrVal + 24 * 60 - depVal;
      }
      return `${Math.floor(discountTime / 60)}时${discountTime % 60}分`;
    }
  },
  methods: {
    handleShowFlightsList() {
      // 低价推荐的显示与隐藏
      this.showFlightsList = !this.showFlightsList;
    }
  }
};
</script>

<style scoped lang="less">
.flight-item {
  border: 1px #ddd solid;
  margin-bottom: 10px;

  .flight-info {
    padding: 15px;
    cursor: pointer;

    > div {
      &:first-child,
      &:last-child {
        text-align: center;
      }
    }
  }

  .flight-info-center {
    padding: 0 30px;
    text-align: center;

    .flight-airport {
      strong {
        display: block;
        font-size: 24px;
        font-weight: normal;
      }
      span {
        font-size: 12px;
        color: #999;
      }
    }

    .flight-time {
      span {
        display: inline-block;
        padding: 10px 0;
        border-bottom: 1px #eee solid;
        color: #999;
      }
    }
  }

  .flight-info-right {
    .sell-price {
      font-size: 24px;
      color: orange;
      margin: 0 2px;
    }
  }
}

.flight-recommend {
  background: #f6f6f6;
  border-top: 1px #eee solid;
  padding: 0 20px;

  .flight-sell {
    border-bottom: 1px #eee solid;
    padding: 10px 0;

    &:last-child {
      border-bottom: none;
    }

    .flight-sell-left {
      font-size: 12px;
      span {
        color: green;
      }
    }

    .price {
      font-size: 20px;
      color: orange;
    }

    .choose-button {
      text-align: center;
      color: #666;
      button {
        display: block;
        width: 100%;
        margin-bottom: 5px;
      }
    }
  }
}
</style>